﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections.Specialized;

namespace PCS.Utilities.Web
{
    public class QueryStringParameter
    {
        private string paramName;
		private string paramValue;

		public QueryStringParameter(string name, string value)
		{
			this.Name = name;
			this.Value = value;
		}

		public string Name
		{
			get { return paramName; }
			set { paramName = value; }
		}

		public string Value
		{
			get { return paramValue; }
			set { paramValue = value; }
		}
    }

    public class QueryStringUtility
	{
        public static byte GetByte(string parameterName)
        {
            return GetByte(parameterName, 0);
        }

        public static int? GetNullableInt(string parameterName)
        {
            int? value = null;
            try
            {
                value = int.Parse(HttpContext.Current.Request.QueryString[parameterName]);
            }
            catch { }
            return value;
        }

        public static bool? GetNullableBool(string parameterName)
        {
            int? value = GetNullableInt(parameterName);
            if (!value.HasValue)
            {
                return null;
            }
            return value.Value == 0 ? false : true;
        }

        public static byte GetByte(string parameterName, byte defaultValue)
        {
            byte result = defaultValue;
            try
            {
                result = byte.Parse(HttpContext.Current.Request.QueryString[parameterName]);
            }
            catch {}			
            return result;
        }

		public static short GetInt16(string parameterName)
		{
			return GetInt16(parameterName, 0);
		}

		public static short GetInt16(string parameterName, short defaultValue)
		{
			short result = defaultValue;
			try
			{
				result = short.Parse(HttpContext.Current.Request.QueryString[parameterName]);
			}
			catch {}			
			return result;
		}

        public static int GetInt32(string parameterName)
        {
            return GetInt32(parameterName, 0);
        }

        public static int GetInt32(string parameterName, int defaultValue)
        {
            int result = defaultValue;
            try
            {
                result = int.Parse(HttpContext.Current.Request.QueryString[parameterName]);
            }
            catch {}			
            return result;
        }

		public static DateTime GetDateTime(string parameterName, string format, DateTime defaultValue)
		{
			DateTime result = defaultValue;
			try
			{
				result = DateTime.ParseExact(HttpContext.Current.Request.QueryString[parameterName], format, null);
			}
			catch {}			
			return result;
		}

		public static DateTime GetDateTime(string parameterName, DateTime defaultValue)
		{
			return GetDateTime(parameterName, "yyyy-MM-dd", defaultValue);
		}

		public static DateTime GetDateTime(string parameterName)
		{
			return GetDateTime(parameterName, DateTime.MinValue);
		}

        public static string GetString(string parameterName, string defaultValue)
        {
            if (HttpContext.Current.Request.QueryString[parameterName] == null)
                return defaultValue;
            else
                return HttpContext.Current.Request.QueryString[parameterName];
        }

        public static string GetString(string parameterName)
        {
            return GetString(parameterName, null);
        }

		public static string BuildQueryString(QueryStringParameter[] parameters)
		{
			string[] values = new string[parameters.Length];
			int index = 0;
			foreach (QueryStringParameter parameter in parameters)
			{
				values[index++] = parameter.Name + "=" + HttpUtility.HtmlEncode(parameter.Value);
			}

			return String.Join("&", values);
		}

        public static string BuildQueryString(NameValueCollection values)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < values.Count; i++)
            {
                sb.Append(i == 0 ? "?" : "&");
                sb.Append(values.Keys[i]);
                sb.Append("=");
                sb.Append(HttpUtility.UrlEncode(values[i]));
            }
            return sb.ToString();
        }
	}
}
